package org.gnyxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.gnyxy.context.BaseContext;
import org.gnyxy.dto.AdminNotPasswordDTO;
import org.gnyxy.dto.AdminPageQueryDTO;
import org.gnyxy.exception.PasswordEditFailedException;
import org.gnyxy.mapper.AdminMapper;
import org.gnyxy.constant.DeleteConstant;
import org.gnyxy.constant.MessageConstant;
import org.gnyxy.dto.AdminDTO;
import org.gnyxy.entity.Admin;
import org.gnyxy.exception.AccountNotFoundException;
import org.gnyxy.result.PageResult;
import org.gnyxy.service.AdminService;
import org.gnyxy.vo.AdminSelectVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    /**
     * login 登录接口
     * @param adminDTO
     * @return
     */
    @Override
    public Admin login(AdminDTO adminDTO) {
        String name = adminDTO.getName();
        String password = adminDTO.getPassword();
//        比对数据库是否存在该用户
        Admin admin = adminMapper.getByName(name);
        if (admin == null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        // 传入的密码加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        // 比对
        if (!password.equals(admin.getPassword())){
            throw new AccountNotFoundException(MessageConstant.PASSWORD_ERROR);
        }
        return admin;
    }

    /**
     * 新增管理员
     * @param adminDTO
     */
    @Override
    public void add(AdminDTO adminDTO) {
        //校验传进来是否为空
        if (adminDTO == null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_IS_NULL);
        }
        //查询数据库是否有重复用户名
        if (adminMapper.getByName(adminDTO.getName()) != null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_IS_EXIST);
        }
        // 密码加密
        adminDTO.setPassword(DigestUtils.md5DigestAsHex(adminDTO.getPassword().getBytes()));
        Admin admin = new Admin();
        // 拷贝至admin
        BeanUtils.copyProperties(adminDTO,admin);
        // 设置时间等
        admin.setCreateTime(LocalDateTime.now())
                .setUpdateTime(LocalDateTime.now())
                .setDeleted(DeleteConstant.IS_NOT_DELETED);
        adminMapper.add(admin);
    }

    /**
     * 分页查询
     * @param adminPageQueryDTO
     * @return PageResult
     */
    @Override
    public PageResult getPage(AdminPageQueryDTO adminPageQueryDTO) {
        // 分页查询
        PageHelper.startPage(adminPageQueryDTO.getPage(),adminPageQueryDTO.getPageSize());
        Page<AdminSelectVO> page = adminMapper.pageQuery(adminPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 更新
     * @param adminNotPasswordDTO
     */
    @Override
    public void update(AdminNotPasswordDTO adminNotPasswordDTO) {
        if (adminNotPasswordDTO == null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_IS_NULL);
        }
        Admin admin = getById(adminNotPasswordDTO.getId());
        if (admin == null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_IS_NULL);
        }
        admin.setName(adminNotPasswordDTO.getName())
             .setUpdateTime(LocalDateTime.now());
        adminMapper.update(admin);
    }

    /**
     * 删除管理员
     * @param id
     */
    @Override
    public void delete(Long id) {
        Admin admin = getById(id);
        if (admin == null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        adminMapper.delete(id);
    }

    /**
     * 根据 id 查找管理员
     * @param id
     * @return
     */
    public Admin getById(Long id){
        if (id == null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        return adminMapper.getById(id);
    }

    /**
     * 设置密码
     * @param password
     */
    @Override
    public void setPassword(String password) {
        if (password == null || password.equals("")){
            throw new PasswordEditFailedException(MessageConstant.PASSWORD_EDIT_FAILED);
        }
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        Long id = BaseContext.getCurrentId();
        adminMapper.updatePassword(password,id);
        // TODO Redis 删除
    }


}
